package com.aurora.oa.work.presenter;


import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.work.bean.ExpenceBean;
import com.aurora.oa.work.view.WorkExpenceMainActivity;
import com.aurora.oa.work.view.WorkExpenceMainActivityView;
import com.facebook.drawee.drawable.RoundedCornersDrawable;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

/**
* Created by Administrator on 2017/03/29
*/

public class WorkExpenceMainActivityPresenterImpl implements WorkExpenceMainActivityPresenter{

    WorkExpenceMainActivityView view;
    RequestHttpModel model;

    public WorkExpenceMainActivityPresenterImpl(WorkExpenceMainActivityView view) {
        this.view = view;
        model = new RequestHttpModelImpl();
    }

    public void getExpencePageData(final int whitchPage){
        model.requestHttpDataString(AuroraConstants.GET_EXPENCE_PAGE_URL + "/" + whitchPage, new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {
                String data = body;
                Gson gson = new Gson();
                Type type = new TypeToken<HttpResult<List<ExpenceBean>>>(){}.getType();
                HttpResult<List<ExpenceBean>> result = gson.fromJson(body, type);
                if(result.obj==null){
                    view.onRequestDataFail("未请求到数据！");
                    return;
                }
                if(whitchPage == 1){
                    view.showPullDownRefresh(result.obj);
                }else{
                    view.showPullUpLoading(result.obj);
                }
            }

            @Override
            public void onRequestDataFail(String message) {
                    view.onRequestDataFail(message);
            }
        });
    }
}